package com.lanlian.cayman.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lanlian.cayman.R;
import com.lanlian.cayman.fragment.MarketFragment;
import com.lanlian.cayman.utils.MResUtil;
import com.lanlian.cayman.utils.NumberManager;
import com.lanlian.commonlib.bean.PositionDetailsInfo;
import com.lanlian.commonlib.data.GoodsBean;
import com.lanlian.utillib.CommonUtils;
import com.lanlian.utillib.NumberUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/15 16:55
 *     desc   : 交易持仓Adapter
 *     version: 1.0
 * </pre>
 */
public class TradePositionAdapter extends BaseQuickAdapter<PositionDetailsInfo, BaseViewHolder> {

    private List<PositionDetailsInfo> mList;

    public TradePositionAdapter(int layoutResId, @Nullable List<PositionDetailsInfo> data) {
        super(layoutResId, data);
        this.mList = data;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder helper, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(helper, position, payloads);
        if (payloads.isEmpty()) {
            onBindViewHolder(helper, position);
        } else {
            if (!CommonUtils.isListEmpty(mList)) {
                PositionDetailsInfo positionDetailsInfo = mList.get(position);
                if (positionDetailsInfo != null) {
                    helper.setText(R.id.tv_current_price, TextUtils.isEmpty(positionDetailsInfo.getCurrentPrice()) ?
                            positionDetailsInfo.getPrice().toString() : positionDetailsInfo.getCurrentPrice());
                    setProfitLoss(helper, positionDetailsInfo);
                }
            }
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, PositionDetailsInfo item) {
        helper.setBackgroundColor(R.id.lay_position, (helper.getLayoutPosition() + 1) % 2 == 0 ?
                mContext.getResources().getColor(R.color.color_bg_text_view_style2) :
                mContext.getResources().getColor(R.color.color_bg_text_view_style3));
        helper.setText(R.id.tv_contract_name, item.getContractName());
        helper.setText(R.id.tv_order_no, item.getlPositionNo().toString());
        helper.setText(R.id.tv_currency_type, item.getCurrency());
        helper.setText(R.id.tv_direction, "1".equals(item.getcPositionFlag()) ? "买" : "卖");
        helper.setText(R.id.tv_position_num, item.getlCurrentAmount().toString());
        helper.setText(R.id.tv_current_price, TextUtils.isEmpty(item.getCurrentPrice()) ?
                item.getPrice().toString() : item.getCurrentPrice());

        if (item.getAveragePrice() != null) {
            helper.setText(R.id.tv_average_price, item.getAveragePrice());
        } else {
            GoodsBean goodsBean = MarketFragment.getRespMarket().getGoods(item.getVcContractCode());
            if (goodsBean != null) {
                double contractMultiplier = Double.parseDouble(goodsBean.contractMultiplier);
                int count = NumberManager.getNumberDecimalDigits(goodsBean.minChange);
                String averagePrice = NumberUtils.roundStr(item.getEnTotalValue() /
                        contractMultiplier / item.getlCurrentAmount(), count + 1, BigDecimal.ROUND_HALF_UP);
                helper.setText(R.id.tv_average_price, averagePrice);
                item.setAveragePrice(averagePrice);
            }
        }

        if ("1".equals(item.getcPositionFlag())) {
            helper.setTextColor(R.id.tv_direction, mContext.getResources().getColor(R.color.color_T7));
        } else {
            helper.setTextColor(R.id.tv_direction, mContext.getResources().getColor(R.color.color_T8));
        }
        setProfitLoss(helper, item);
    }

    private void setProfitLoss(BaseViewHolder helper, PositionDetailsInfo item) {
        if (item.getProfitLoss() > 0) {
            helper.setText(R.id.tv_profit_loss, String.format("+%.2f", item.getProfitLoss()));
            helper.setTextColor(R.id.tv_profit_loss, MResUtil.getUpColor());
        } else if (item.getProfitLoss() < 0) {
            helper.setText(R.id.tv_profit_loss, String.format("%.2f", item.getProfitLoss()));
            helper.setTextColor(R.id.tv_profit_loss, MResUtil.getDownColor());
        } else {
            helper.setText(R.id.tv_profit_loss, String.format("%.2f", item.getProfitLoss()));
            helper.setTextColor(R.id.tv_profit_loss, MResUtil.getPingColor());
        }
    }

}
